module AboutYou
  module SDK
    module Model
      ###
      # This class is an abstract model of a basket item
      #
      # author:: Collins GmbH & Co KG
      ###
      module AbstractBasketItem
        # determines if the image url is required
        IMAGE_URL_REQUIRED = false

        # Additional data
        attr_accessor :additional_data
        # determines whether a basket item is changed or not
        attr_accessor :is_changed

        ###
        # gets the description of the basket item
        #
        # * *Returns* :
        #   - a String of null if no description is set
        ###
        def description
          additional_data['description'] if additional_data
        end

        ###
        # sets additional data for the basket item
        #
        # * *Args*    :
        #   - +additional_data+ -> the desired data to set
        ###
        def addition_data=(additional_data)
          check_additional_data(additional_data)
          self.is_changed = true

          self.additional_data = additional_data
        end

        ###
        # checks if certain additional_data is valid to set for a basket item or not
        #
        # * *Args*    :
        #   - +additional_data+ -> the desired data to check
        #
        # * *Fails* :
        #   - if additional_data doesnt have a description
        #   - if additional_data doesnt have an image url and it would be required
        ###
        def check_additional_data(additional_data = nil)
          return unless additional_data || IMAGE_URL_REQUIRED
          fail 'InvalidArgumentException! description is required
            in additional data' unless additional_data.key?('description')
          fail 'InvalidArgumentException! image_url is required in
            additional data' if
            IMAGE_URL_REQUIRED && !additional_data.key?('image_url')
        end
      end
    end
  end
end
